$(function() {
	// index and contents
	$('#toc-tabs li').click(function(event) {
		var css = 'tab-selected';
		var tabs = $(this).addClass(css).attr('rel').split('|');
		$('#' + tabs[0]).show();
		$('#' + tabs[1]).hide();
		$('[rel="' + tabs[1] + '|' + tabs[0] + '"]').removeClass(css);
	});

	// tag (label) search
	var $list = $('#index-list');
	$('#tag-table td a').click(function(event) {
		event.preventDefault();
		$list.slideUp(200);
		var url = $(this).attr('href');
		$.get(url, function(data) {
			$list.empty()
				.append($('<legend>').text(url.substr(url.lastIndexOf('/') + 1)));
			
			$(data).find("entry").each(function() {
				$('<div>')
					.addClass('index-item')
					.append(
						$('<div>')
							.addClass('index-date')
							.text(formatDate($('published', this).text().substr(0, 10)))
					)
					.append(
						$('<a>')
							.attr('href', $('link[rel="alternate"]', this).attr('href'))
							.text($('title', this).text())
					)
					.appendTo($list);
			});
			$list.slideDown();
		}, 'xml');
	});
});
	/*
	var flickr = {
		api: 'http://api.flickr.com/services/rest/?format=json',
		key: 'bf8fcf08b9a810415f8a0a33a7a502cf',
		salt: '16a9b54c98d47ae3',
		frob: null,
		token: '72157625260030434-4bc5035abf536dcc',
		permission: 'read',
		authenticated: false,
		callName: 'jsoncallback',
		page: 'http://flickr.com/photo.gne?id=',
		get: {
			sizes: 'flickr.photos.getSizes',
			location: 'flickr.photos.geo.getLocation',
			exif: 'flickr.photos.getExif',
			frob: 'flickr.auth.getFrob',
			token: 'flickr.auth.getToken'
		},
		url: function(photoID, methodName) {
			if (methodName) {
				var link = this.api
					+ '&api_key=' + this.key
					+ '&method=' + methodName;
				if (photoID) { link += '&photo_id=' + photoID; }
				return this.sign(link, this.getJsonFn(photoID, methodName));
			} else {
				return this.page + photoID;
			}
		},
		service: function(photoID, methodName, fn) {
			if (!this.token) {
				$.getJSON(this.url(null, this.get.frob), function(r) {
					alert('frob' + r.message);
				});
			} else {
				$.ajax({
					cache: true,
					url: this.url(photoID, methodName),
					dataType: 'jsonp',
					jsonp: this.callName,
					jsonpCallback: this.getJsonFn(photoID, methodName),
					data: null,
					error: function(xhr, status, ex) { alert(status); },
					success: fn
				});
			}
		},
		parseExif: function(data) {
			if (!data || !data.length) { return null; }
			var allow = /(IFD0|ExifIFD|XMP-tiff|XMP-aux)/;
			var exif = new Object();
			for (var i = 0; i < data.length; i++) {
				if (allow.test(data[i].tagspace)) {
					exif[data[i].label] = data[i].raw._content;
				}
			}
			return exif;
		},
		getJsonFn: function(photoID, methodName) {
			return methodName.substr(methodName.lastIndexOf('.') + 1) + photoID;
		},
		sign: function(url, jsonFn) {
			if (this.token) { url += '&auth_token=' + this.token; }
			var re = /[\?&]([^&=\?]+=[^&=]+)/g;
			var params = [this.callName + '=' + jsonFn];
			var match;
			while (match = re.exec(url)) { params.push(match[1]); }
			params.sort();
			return url + '&api_sig=' + hex_md5(this.salt + params.toString().replace(/[=,]/g, ''));
		},
		login: function() {
			var url = 'http://flickr.com/services/auth/'
				+ '?api_key=' + this.key
				+ '&perms=' + this.permission
				+ '&frob=' + this.frob
			location.href = this.sign(url);
		}
	};
	
	$(".photo").hover(
		function(event) {
			var $img = $('img', this);
			var data = $img.data("flickr");
			
			if (!data) {
				var url = $img.attr("src");
				var match = /\/(\d{10})_/.exec(url);
				
				data = {
					photoID: match[1],
					page: flickr.url(match[1]),
					geo: null,
					size: null,
					exif: null,
					ready: false
				};
				// get sizes
				flickr.service(data.photoID, flickr.get.sizes, function(r) {
					if (r.stat == 'ok') {
						var list = r.sizes.size;
						data.size = new Object();
						for (var i = 0; i < list.length; i++) {
							data.size[list[i].label] = list[i].source;
						}
					}
					// get exif
					flickr.service(data.photoID, flickr.get.exif, function(meta) {
						if (meta.stat == 'ok') { data.exif = flickr.parseExif(meta.photo.exif); }
					
						// get location
						flickr.service(data.photoID, flickr.get.location, function(gps) {
							if (gps.stat == 'ok') { 
								data.geo = {
									longitude: gps.photo.location.longitude,
									latitude: gps.photo.location.latitude
								};
							}
							$img.data("flickr", data);
							showImageOverlay($img);
						});
					});
				});
			} else {
				showImageOverlay($img);
			}
		},
		function(event) {
			$('.photo-overlay', this).hide();
		}
	).click(function(event) {
		location.href = $(this).data("flickr").page;
	});
	
	function debug(message) {
		// add message to by-line
		$(".post h6").append('<br/><code>' + message + '</code>');
	}
	
	function showImageOverlay($img) {
		var data = $img.data("flickr");
		// http://l.yimg.com/g/images/iphone/balls-24x12-trans.gif
		if (!data.ready) {
			var html = '<div class="photo-overlay">';
			
			if (data.exif) {
				html += '<ul class="exif">';
				if (data.exif['Model']) {
					html += '<li class="model">Taken with a ' + data.exif['Lens']
						 + ' lens<br/>on a ' + data.exif['Model'] + '</li>';
				}
				if (data.exif['Exposure'] && data.exif['Aperture']) {
					html += '<li class="exposure">F/' + data.exif['Aperture']
						  + ' for ' + data.exif['Exposure'] + ' second'
						  + ' at ISO ' + data.exif['ISO Speed'] + '</li>';
				}
				html += '</ul>';
			}
			
			html += '<ul class="photo-links">';
			
			if (data.size) {
				html += '<li class="enlarge-link"><a href="' + data.size['Original'] + '">Enlarge</a></li>';
			}
			if (data.page) {
				html += '<li class="flickr-link"><a href="' + data.page + '">Flickr Page</a></li>';
			}
			// http://maps.google.com/maps?f=q&source=s_q&hl=en&geocode=&q=44.931098,-116.029443&ie=UTF8&ll=44.928349,-116.025667&spn=0.022757,0.034118&t=h&z=15
			if (data.geo) {
				html += '<li class="map-link"><a href="http://maps.google.com/maps?f=q&source=s_q&hl=en&geocode=&q='
					 + data.geo.latitude + ',' + data.geo.longitude + '&t=h&z=15">Map</a></li>';
			}
			html += '</ul></div>';
			
			$img.parent().append(html);
			data.ready = true;
		}
		$('.photo-overlay', $img.parent()).fadeTo(400, 0.6);
	}
});

function formatDate(d) {
	var part = d.split('-');
	var month = ['January','February','March','April','May','June','July','August','September','October','November','December'];
	return month[(part[1] * 1) - 1] + ' ' + (part[2] * 1) + ', ' + part[0];
}
*/